﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sdamarketing.App_Code;

namespace sdamarketing.Administration
{
    public partial class ChangeUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // AN Go krijam panelot dodeka ne e selektiran korisnik koj treba da se izmeni
            PanelUsersDetails.Enabled = false;

            if (!IsPostBack)
            {
                try
                {
                    if (Session["iduser"] == null || Session["username"] == null)
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                    else
                    {
                        if (RolesAndPrivileges.AuthenticateUserWithPanel("Менаџирај корисници", Int32.Parse(Session["idRole"].ToString())) == 0)
                        {
                            Response.Redirect("~/Home.aspx");
                        }
                    }

                    // AN Go polnam gridot so site korisnici od baza (za da ne bide prazen na start) ova moze i da se izmeni
                    DataSet dataSet = Users.Search("", "", "", "");
                    UsersGridView.DataSource = dataSet.Tables["DataSet"];
                    UsersGridView.DataBind();
                    
                    DataSet roles = ReturnDataSet.ReturSet("select name, idRole FROM Roles");
                    PrivilegesDropDownList.DataValueField = "idRole";
                    PrivilegesDropDownList.DataTextField = "name";
                    PrivilegesDropDownList.DataSource = roles;
                    PrivilegesDropDownList.DataBind();
                    PrivilegesDropDownList.Items.Insert(0, new ListItem("", ""));
                }
                catch (Exception exception)
                {
                    SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -changeUser- ");   
                }
            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            try
            {
                // AN Go polnam gridot spored vnesenite search kriteriumite
                DataSet dataSet = Users.Search(TextBoxName.Text, TextBoxLastName.Text, TextBoxUsername.Text, PrivilegesDropDownList.SelectedValue.ToString());
                UsersGridView.DataSource = dataSet.Tables["DataSet"];
                UsersGridView.DataBind();

                // gi praznam text boxovite, zatoa sto pravime nov search
                TextBoxUsernameEdit.Text = "";
                FirstNameTextBox.Text = "";
                LastNameTextBox.Text = "";
                EMailTextBox.Text = "";
                PrivilegesDropDownListEdit.ClearSelection();
                BlockedCheckBox.Checked = false;
            }
            catch (Exception exception)
            {
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -changeUser- ");    
            }
        }

        protected void UsersGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // AN imam selektirano korisnik go prikazuvam panelot
                PanelUsersDetails.Enabled = true;
                TextBoxUsernameEdit.Enabled = false;
                EMailTextBox.Enabled = false;

                // AN Go polnam drop down Priviliges od baza
                DataSet roles = ReturnDataSet.ReturSet("select name, idRole FROM Roles");
                PrivilegesDropDownListEdit.DataValueField = "idRole";
                PrivilegesDropDownListEdit.DataTextField = "name";
                PrivilegesDropDownListEdit.DataSource = roles;
                PrivilegesDropDownListEdit.DataBind();

                /*          
                        // AN Gi polnam text boxovite za ime, prezime, korisnicko ime bez da odam do baza (OD Gridot) 
                            TextBoxUsernameEdit.Text = UsersGridView.SelectedRow.Cells[3].Text;
                            FirstNameTextBox.Text = UsersGridView.SelectedRow.Cells[1].Text;
                            LastNameTextBox.Text = UsersGridView.SelectedRow.Cells[2].Text;
                 */
                // AN Gi polnam text boxovite se vrakam do baza da gi zemam
                DataSet user = ReturnDataSet.ReturSet("SELECT firstName, lastName, eMail, username, isBlock, idRole FROM Users WHERE username='" + UsersGridView.SelectedRow.Cells[2].Text + "'");
                TextBoxUsernameEdit.Text = user.Tables["DataSet"].Rows[0].ItemArray[3].ToString();
                FirstNameTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[0].ToString();
                LastNameTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[1].ToString();
                EMailTextBox.Text = user.Tables["DataSet"].Rows[0].ItemArray[2].ToString();
                if (user.Tables["DataSet"].Rows[0].ItemArray[4].ToString() == "True")
                {
                    BlockedCheckBox.Checked = true;
                }
                else
                    BlockedCheckBox.Checked = false;

                // JA SETIRAM ROLATA NA USEROT PO DEFAULT DA BIDE CHEKIRANA 
                PrivilegesDropDownListEdit.SelectedIndex = PrivilegesDropDownListEdit.Items.IndexOf(PrivilegesDropDownListEdit.Items.FindByValue(user.Tables["DataSet"].Rows[0].ItemArray[5].ToString()));
            }
            catch (Exception exception)
            {
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -changeUser- ");    
            }
                               
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                // AN gi zacuvuvam izmenite 
                Users.EditUser(UsersGridView.SelectedRow.Cells[2].Text, FirstNameTextBox.Text, LastNameTextBox.Text, Int32.Parse(PrivilegesDropDownListEdit.SelectedValue.ToString()), BlockedCheckBox.Checked.ToString());
                // AN Go refreshuvam gridot za da moze da se vidat novite ismeni
                SaveHistory.addHystoriForUser(Int32.Parse(Session["idUser"].ToString()), Users.FindUser(UsersGridView.SelectedRow.Cells[2].Text), 2);

                DataSet dataSet = Users.Search(TextBoxName.Text, TextBoxLastName.Text, TextBoxUsername.Text, PrivilegesDropDownList.SelectedValue.ToString());
                UsersGridView.DataSource = dataSet.Tables["DataSet"];
                UsersGridView.DataBind();
            }
            catch (Exception exception)
            {
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -changeUser- ");    
            }
        }

        protected void UsersGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                UsersGridView.PageIndex = e.NewPageIndex;
                UsersGridView.SelectedIndex = -1;

                DataSet dataSet = Users.Search(TextBoxName.Text, TextBoxLastName.Text, TextBoxUsername.Text, PrivilegesDropDownList.SelectedValue.ToString());
                UsersGridView.DataSource = dataSet.Tables["DataSet"];
                UsersGridView.DataBind();
            }
            catch (Exception exception)
            {
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -changeUser- ");    
            }            
        }

        protected void UsersGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                Users.isDeleteUser(UsersGridView.Rows[e.RowIndex].Cells[2].Text);
                SaveHistory.addHystoriForUser(Int32.Parse(Session["idUser"].ToString()), Users.FindUser(UsersGridView.Rows[e.RowIndex].Cells[2].Text), 3);

                DataSet dataSet = Users.Search("", "", "", "");
                UsersGridView.DataSource = dataSet.Tables["DataSet"];
                UsersGridView.DataBind();
            }
            catch (Exception exception)
            {
                SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -changeUser- ");    
            }
        }
    }
}
